<template>
  <div class="layui-layout layui-layout-admin">
    <div class="adminnav1">
      <Pagetop></Pagetop>
    </div>
    <div class="adminnav2">
      <Secondnav urlname="jiesuan"></Secondnav>
    </div>

    <!-- 主体内容 -->
    <div class="layui-body kedou-admin" id="LAY_app_body app">
      <div class="layadmin-tabsbody-item layui-show">
        <div class="pagelist">
        	<div class="layui-card-body">
		      		<fieldset class="layui-elem-field layui-field-title">
							  <legend>可结算订单列表</legend>
							</fieldset>
		      </div>
        	<div class="pagetop">
	    			<div class="layui-form layui-card-header layuiadmin-card-header-auto">
		    			<div class="layui-form-item">
			        	    <div class="layui-inline">
				              	<label class="layui-form-label">开始时间</label>
				              	<div class="layui-input-inline">
				                	<input type="text" class="layui-input b" onfocus='this.blur()' readonly="readonly" id="beginAt" v-model="beginAt" placeholder="请选择">
				              	</div>
				            </div>
				            <div class="layui-inline">
				              	<label class="layui-form-label">结束时间</label>
				              	<div class="layui-input-inline">
				                	<input type="text" class="layui-input b" onfocus='this.blur()' readonly="readonly" id="endAt" v-model="endAt" placeholder="请选择">
				              	</div>
				            </div>
					        <div class="layui-inline">
					            <button class="layui-btn layuiadmin-btn-useradmin"  @click="sure">提交结算申请</button>
					        </div>		            
							</div>
						</div>		 		    	
		      </div>		  
          <div class="layui-card-body">
            <div class="buttop">
              <button type="button" class="layui-btn layui-btn-normal" @click="lookData">查看未结算统计</button>
            </div>
            <Tables
              :fdate="pdate"
              :result="res"
              height="430"
              :totalCount="totalRows"
              :pageNumber="curindex"
              :fbtn="btn"
            ></Tables>
          </div>
        </div>
      </div>
    </div>
    <div class="tjlist" style="display:none;">
    		<div class="tjli" v-for="item in tjlist">
    			<div class="tjtit">{{item.type==1?'今日':'往日'}}未结算统计</div>
	    		<div class="tjcon">
	    			<ul>
	    				<li>订单金额:{{item.payAmount/100}}</li>
	    				<li>订单笔数:{{item.payCount}}</li>
	    				<li>退款金额:{{item.refundAmount/100}}</li>
	    				<li>退款笔数 :{{item.refundCount}}</li>
	    			</ul>
	    		</div>
    		</div>
    </div>
  </div>
</template>
<script>
import Pagetop from "../common/pagetop.vue";
import Secondnav from "../common/secondnav.vue";
import Tables from "../common/tables.vue";
import Search from "../common/search.vue";
export default {
  name: "gradelist",
  components: { Pagetop, Secondnav, Tables, Search },
  data() {
    return {
      pdate: [], //表头
      res: "", //内容
      totalRows: "", //总数量
      curindex: 1, //页数
      btn: [], //按钮
      searchArr: [], //搜索
      isShow: false,
      seapram: "",
      beginAt:'',
      endAt:'',
      type: localStorage.getItem("type"),
      tjlist:[]
    };
  },
  mounted() {
  	var _this=this;
  	layui.use(['form','laydate'], function(){
		  var form = layui.form,
		  laydate = layui.laydate;
		  laydate.render({
	    	elem: '#beginAt',//指定元素
	    	type: 'datetime',
	    	btns: ['now', 'confirm'],
			  format: 'yyyy-MM-dd HH:mm:ss',
	    	done:function(value, date, endDate){
				_this.beginAt = value;
				//alert(getUTCtime(value))
	    	}
	  	  });
		  laydate.render({
	    	elem: '#endAt',//指定元素
	    	type: 'datetime',
	    	btns: ['now', 'confirm'],
			  format: 'yyyy-MM-dd HH:mm:ss',
	    	done:function(value, date, endDate){
				_this.endAt = value;
	    	}
		  });
		});	
    this.getdate();
    this.getsearch();
  },
  methods: {
    getdate: function () {
      var _this = this;
      var url = "/pay/bapi/v1.9/settlement/settlementPage";
      if (_this.seapram) {
        var dates = {
          size: pageSize,
          page: _this.curindex,
          //sort:'createAt,DESC',
          //'status.equals':'ENABLE'
        };
        var res = _this.seapram;
        for (var i = 0; i < res.length; i++) {
          dates[res[i].key + ".equals"] = res[i].val;
        }
      } else {
        var dates = {
          size: pageSize,
          page: _this.curindex,
          //sort:'createAt,DESC',
          //'status.equals':'ENABLE'
        };
      }
      //alert(url+"//"+JSON.stringify(dates))
      ajaxRequest(url, "GET", "json", dates, function (ret, err) {
        //alert(url+"//"+JSON.stringify(ret))
        var arr = [];
        arr[0] = [
          { field: "businessOrderId", title: "交易订单号" },
          { field: "createAt", 
            title: "交易时间",
          	templet: function (d) {
              return FormatDate(d.createAt)
            }
          },
          {
            field: "amount",
            title: "交易金额",
            templet: function (d) {
              return d.payAmount / 100;
            },
          },
          {
            field: "shopName",
            title: "交易店铺",
          },
          {
            field: "note",
            title: "交易备注",
          },
          {
            field: "ownerType",
            title: "收入方类型",
            templet: function (d) {
              return d.ownerType==1?'门店':'个人';
            }
          },
          {
            field: "payMethod",
            title: "到帐类型 ",
            templet: function (d) {
              if(d.payMethod =='MERCHANDIRECTTPAY'){
              	return '特约商户直接到帐'
              }
              else if(d.payMethod=='PLATFORMPAY'){
              	return '平台普通商户结算'
              }
              else if(d.payMethod=='PLATFORMERCHANDIRECTMPAY'){
              	return '平台特约商户结算'
              }
            }
          },
          {
            field: "settleAmount",
            title: "结算总额 ",
            templet: function (d) {
              return d.settleAmount/100;
            }
          },
          {
            field: "tradeTypeId",
            title: "交易分类",
            templet: function (d) {
              if(d.tradeTypeId=='1001'){
              	return '商品消费'
              }
              else if(d.tradeTypeId=='1002'){
              	return '活动买单消费'
              }
              else if(d.tradeTypeId=='1004'){
              	return '开户费'
              }
              else if(d.tradeTypeId=='0011'){
              	return '开通会员'
              }
              else if(d.tradeTypeId=='1005'){
              	return '充值'
              }
              else if(d.tradeTypeId=='2003'){
              	return '分账'
              }
            }
          },
          {
            field: "settlementStatus",
            title: "结算状态",
            templet: function (d) {
              if(d.settlementStatus=='WAITSETTLE'){
              	return '未结算'
              }
              else if(d.settlementStatus=='HASBEENSETTLED'){
              	return '已结清'
              }
              else if(d.settlementStatus=='SETTLING'){
              	return '未结清'
              }
              else if(d.settlementStatus=='NOSETTLE'){
              	return '无结算'
              }
              else if(d.settlementStatus=='REFUND'){
              	return '已退回'
              }
              
            }
          },
        
        ];
        _this.pdate = arr;
        _this.res = ret.data.list; //res1;
        _this.totalRows = ret.data.totalRows;
        _this.curindex = ret.data.page + 1;
      });
    },
    getsearch: function () {
      var arr = [{ field: "id", title: "ID", type: "input" }];
      this.searchArr = arr;
    },
    page: function (index, obj) {
      //alert(JSON,stringify(obj))
      this.curindex = index;
      this.getdate();
    },
    sousuo: function (pram) {
      //alert(JSON.stringify(pram))
      this.seapram = pram;
      this.res = "";
      this.totalRows = "";
      this.curindex = "";
      this.getdate();
    },
    sure:function(){
    	var url="/pay/bapi/v1.9/settlement/getDayTradeBeginEndDate"
    	if(!this.beginAt||!this.endAt){
    		 layer.msg("请选择开始跟结束时间");
    		return false
    	}
    	var dates={
			  "beginAt": getsjc(this.beginAt),
			  "endAt":getsjc(this.endAt),
			  "ownerType":1
			}
    	var _this=this;
      ajaxRequest(url, 'POST', 'json',dates, function(ret, err){			 
      	layer.alert("应结订单金额："+ret.data.orderPayAmount/100+"，手续费："+ret.data.serviceCharge/100+"，应结算总额："+ret.data.settleAmount /100,function (index) {
				  var index = parent.layer.getFrameIndex(window.name);
					parent.layer.closeAll(index);
				  if(ret.data.settleAmount>0){
				  
						var url1="/pay/bapi/v1.9/settlement"
					  ajaxRequest(url1, 'POST', 'json',dates, function(ret, err){
					  	layer.msg("申请成功，请添加银行卡信息");
					  	setTimeout(function(){
							  	 
							      layer.confirm('是否新增银行卡？', {
										  btn: ['新增银行卡','选择已有的卡'] //按钮
										}, function () {
											layer.open({
								        type: 2,
								        title: "新增银行卡信息",
								        content: baseurl + "/yhkadd",
								        area: _this.screens() < 2 ? ["80%", "65%"] : ["800px", "650px"],
								        success: function (layero, index) {
								         	var iframeWin = window[layero.find('iframe')[0]['name']];
							        		iframeWin.getdates(data);
								        },
								      });
										}, function(){
											layer.open({
								        type: 2,
								        title: "选择银行卡信息",
								        content: baseurl + "/juesuanadd",
								        area: _this.screens() < 2 ? ["80%", "65%"] : ["800px", "650px"],
								        success: function (layero, index) {
								         	var iframeWin = window[layero.find('iframe')[0]['name']];
							        		iframeWin.getdates(ret.data);
								        },
								      });
										});
				      },1000)
					  })
					}
				  else{
				  	layer.msg("申请金额为0，不需要提交审核");
				  }
			 })
      	
		  })
    },
    lookData: function () {
      var url='/pay/bapi/v1.9/settlement/settlementShopId'
      var dates={
      	ownerType:1
      }
      var _this=this;
      ajaxRequest(url, 'GET', 'json',dates, function(ret, err){			 		
      	_this.tjlist=ret.data.settlementAmountList
      	setTimeout(function(){
      		layer.open({
					  type: 1,
					  shade: false,
					  title: false, 
					  content:$(".tjlist"), 
					  cancel: function(){
					    
					  }
					});
      	},600)
		  	
		  })
      
    },
    screens: function () {
      return screen();
    },
  },
  updated: function () {
    this.isShow = true;
    layui.use("form", function () {
      var form = layui.form;
      form.render();
    });
  },
};
</script>
<style scoped>
	.tjlist{padding:20px 0;}
	.tjlist .tjtit{padding:0 10px 10px 10px;}
	.tjcon{border-top:1px solid #eee;padding:0 20px 20px 20px}
	.tjcon li{display:block;padding:10px 20px 0 20px; text-align: left;}
</style>